<template>
  <div class="question" v-if="info !== ''">
    <!-- 下拉菜单 
   van-dropdown-menu
       van-dropdown-item  
           v-model:选中后的value
           options:对应下拉选项[{text:显示文本,value:对应选中后的值}]
           slot="title/default"
           toggle(true:打开选项面板,false:关闭面板,通过ref调用该方法) 
           索引栏
    van-index-bar index-list:索引数组
       van-index-anchor index="索引值"  
   -->
    <van-dropdown-menu>
      <van-dropdown-item ref="drop">
        <template #title>
          <div class="title">
            面试宝典<span class="city">{{ city }}</span>
          </div>
        </template>
        <template #default>
          <van-index-bar :index-list="indexList">
            <!-- v-for用于对象
            {key:value}
            v-for="(value,key,index(从0开始的序号)) in 对象名"
             -->
            <div v-for="(value, key, index) in info.citys" :key="index">
              <van-index-anchor :index="key"
                ><span class="anchor">{{ key }}</span></van-index-anchor
              >
              <van-cell
                :title="item"
                v-for="(item, index2) in value"
                :key="index2"
                @click.native="cityClick(item)"
              ></van-cell>
            </div>
          </van-index-bar>
        </template>
      </van-dropdown-item>
    </van-dropdown-menu>
    <div class="content">
      <div class="tag-list">
        <van-tag
          v-for="(item, index) in info.cityPositions[city]"
          :key="index"
          class="tag"
          :class="{ active: currIndex === index }"
          color="#f7f4f5"
          text-color="#545671"
          @click="currIndex = index"
          >{{ item.name }}</van-tag
        >
      </div>
    </div>
    <!-- 中间 -->
    <div class="line"></div>
    <div class="middle">
      <div class="middle-top">
        <div class="item">
          <div class="item-top color1">
            <i class="iconfont">&#xe64f;</i>
          </div>
          <div class="item-bottom">常错题库</div>
        </div>
        <div class="item">
          <div class="item-top color2">
            <i class="iconfont">&#xe64f;</i>
          </div>
          <div class="item-bottom">常错题库</div>
        </div>
      </div>
      <div class="middle-bottom">
        <div class="item">
          <div class="item-top color3">
            <i class="iconfont">&#xe64f;</i>
          </div>
          <div class="item-bottom">常错题库</div>
        </div>
        <div class="item">
          <div class="item-top color4">
            <i class="iconfont">&#xe64f;</i>
          </div>
          <div class="item-bottom">常错题库</div>
        </div>
      </div>
      <div class="center">
        <!-- van-circle
         v-model:当前值
         rate:目标值最大100
         speed: 多少rate/秒 
         layer-color:轨道颜色  
         -->
        <van-circle
          class="circle"
          v-model="circle"
          :rate="
            ($store.state.userInfo.correctQuestions.length * 100) /
              info.totalCount
          "
          :speed="10"
          layer-color="none"
          :color="color"
        >
          <div class="txt">
            <div class="txt1">顺序刷题</div>
            <div class="txt2">
              {{ userData.correctQuestions.toFixed(0) }}/{{
                userData.totalCount.toFixed(0)
              }}
            </div>
            <!--  {{ $store.state.userInfo.correctQuestions.length }}/{{
                info.totalCount
              }} -->
          </div>
        </van-circle>
      </div>
    </div>
    <div class="line"></div>
    <div class="result">
      <div class="result1">累计收录</div>
      <div class="result2">
        {{ userData.totalCount.toFixed(0) }}
      </div>
    </div>
    <div class="btn">
      <van-button block color="#00b8d4" @click="goQuestionInfo"
        >模拟面试</van-button
      >
    </div>
  </div>
</template>
<script>
import { interviewFilters } from '@/api/question'
import gsap from 'gsap'
import { mapState } from 'vuex'
export default {
  data () {
    return {
      indexList: [],
      info: '',
      city: '全国',
      // indexList: ['A', 'B', 'C'],
      currIndex: 0,
      circle: 0,
      // border-image: linear-gradient(135deg, rgba(228,1,55,0.3), rgba(228,1,55,0.7) 99%) 13 13;

      color: {
        '0%': 'rgba(228,1,55,0.7)',
        '100%': 'rgba(228,1,55,0.3)'
      },
      // 本地数据
      userData: {
        correctQuestions: 0,
        totalCount: 0
      }
    }
  },
  computed: {
    ...mapState(['userInfo'])
  },
  // async activated () {},

  async created () {
    this.$toast.loading({
      duration: 0,
      message: '加载中'
    })
    const res = await interviewFilters()
    this.indexList = Object.keys(res.data.data.citys)
    this.info = res.data.data
    this.$toast.clear()

    // 数字
    gsap.to(this.$data.userData, {
      duration: 1.5,
      correctQuestions: this.userInfo.correctQuestions.length,
      totalCount: this.info.totalCount
    })
    window.console.log(this.info.totalCount)
    // 进度
    const targetRate =
      (this.userInfo.correctQuestions.length / this.info.totalCount) * 100
    gsap.to(this.$data, { duration: 1.5, rate: targetRate })
  },
  computed: {
    ...mapState(['userInfo'])
  },
  methods: {
    cityClick (str) {
      this.city = str
      this.$refs.drop.toggle(false)
      this.currIndex = 0
    },
    goQuestionInfo () {
      this.$router.push({
        path: '/home/questionInfo',
        // name: 'questionInfo',
        query: {
          city: this.city,
          id: this.info.cityPositions[this.city][this.currIndex].id
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.question {
  background-color: #f7f4f5;
  .title {
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Semibold;
    font-weight: 600;
    text-align: left;
    color: #222222;
    line-height: 25px;
    letter-spacing: 0px;
  }
  .city {
    font-size: 12px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: center;
    color: #545671;
    line-height: 16px;
  }
  .anchor {
    font-weight: 600;
  }
  .content {
    padding: 0 @p15;
    background-color: #fff;
    .tag-list {
      width: 100%;
      overflow: auto;
      display: flex;
      padding: 10px 0;
    }
    .tag {
      padding: 5px 8px;
      border-radius: 10px;
      margin-right: 15px;
      flex: 0 0 auto;
    }
    .active {
      background-color: #00b8d4 !important;
      color: #fff !important;
    }
  }
  .line {
    height: 15px;
    background-color: #f7f4f5;
  }
  .middle {
    padding: 16px 40px;
    position: relative;
    background-color: #fff;
    .middle-top {
      display: flex;
      justify-content: space-between;
    }
    .middle-bottom {
      display: flex;
      justify-content: space-between;
      margin-top: 33px;
    }
    .item {
      width: 50px;
      text-align: center;
      .item-top {
        display: inline-block;
        width: 44px;
        height: 44px;

        text-align: center;
        line-height: 44px;
        color: #fff;
        border-radius: 50%;
        .iconfont {
          font-size: 30px;
        }
      }
      .color1 {
        background: linear-gradient(180deg, #ff8080, #ff4949);
      }
      .color2 {
        background: linear-gradient(180deg, #ffda05, #ffb302);
      }
      .color3 {
        background: linear-gradient(180deg, #00ddec, #00b8d4);
      }
      .color4 {
        background: linear-gradient(180deg, #3ee5b1, #1dc779);
      }
      .item-bottom {
        font-size: 12px;
        margin-top: 10px;
      }
    }
    .center {
      width: 120px;
      height: 120px;
      // 在html与js中@表示 src,在css中~@表示src
      background: url(~@/assets/circle.png) no-repeat;
      background-size: 100%;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      .circle {
        width: 100% !important;
        height: 100% !important;
        padding: 10px;
      }
      .txt {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background: linear-gradient(180deg, #ff6f92, #e40137);
        color: #fff;
        font-size: 16px;
        padding-top: 30px;
      }
    }
  }
  .result {
    text-align: center;
    background-color: #fff;
    .result1 {
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Semibold;
      font-weight: 600;
      color: #545671;
      line-height: 25px;
      letter-spacing: 0px;
      padding: 21px 0;
    }
    .result2 {
      font-size: 30px;
      font-family: PingFangSC, PingFangSC-Semibold;
      font-weight: 600;
      color: #545671;
      line-height: 42px;
      letter-spacing: 1px;
      padding-bottom: 20px;
    }
  }
  .btn {
    padding: 20px @p15 5px;
  }
}
</style>
